<%--
  Created by IntelliJ IDEA.
  User: c
  Date: 2018/8/27
  Time: 19:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>修改维修记录</title>
    <link rel="stylesheet" href="/frame/layui/css/layui.css">
    <link rel="stylesheet" href="/frame/static/css/style.css">
</head>
<body class="body">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>新增维修记录</legend>
</fieldset>

<form class="layui-form">

    <input id="maiid" name="maiid" type="text" hidden="hidden">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">服务时长</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="mailength" id="mailength" lay-verify="number" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">服务人员</label>
            <div class="layui-input-inline">
                <select name="staId" id="staId" lay-verify="required">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">费用</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="maimoney" id="maimoney" lay-verify="number" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">提车时间</label>
            <div class="layui-input-inline">
                <input type="text" name="maidate" id="maidate" lay-verify="datetime" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="../js/jquery.js"></script>
<script src="../frame/layui/layui.js" charset="utf-8"></script>
<script src="../js/dateToString.js"></script>
<script>
    $(function () {
        // 加载员工
        $.ajax({
            url: "/Staff/selectAll",
            type: "POST",
            dataType:"json",
            async:false,
            success: function(data){
                console.log(data);
                if(data.success){
                    var staffs = data.data;
                    // 判断是否有数据
                    if(staffs.length>=0) {
                        for (var i = 0; i < staffs.length; i++) {
                            $("#staId").append("<option value='"+staffs[i].staid+"'>"+staffs[i].staname+"</option>");
                        }
                    }else{
                        layer.msg("未查询到数据！");
                    }
                }else{
                    // 错误提示
                    layer.msg(data.message);
                }
            }
        });

        // 获取编号
        var maiid=window.location.search.slice(window.location.search.lastIndexOf("?")+1);
        // 给隐藏框赋值
        $("#maiid").val(maiid);
        // 加载数据
        $.ajax({
            url: "/Maintain/queryById",
            type: "POST",
            dataType:"json",
            async:false,
            data: {maiid:maiid},
            success: function(data){
                console.log(data);
                // 赋值
                $("#mailength").val(data.data.mailength);
                $("#staId").val(data.data.staff.staid);
                $("#maimoney").val(data.data.maimoney);
                $("#maidate").val(dateToStr(data.data.maidate));
            }
        });
    });
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#maidate',
            type:'datetime'
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            update(data);
            return false;
        });
        form.render('select');
    });
    // 修改数据
    function update(data){
        // 获取需要的值
        var maiid = data.field.maiid;
        maiid = parseInt(maiid);
        var mailength = data.field.mailength;
        mailength = parseInt(mailength);
        var staId = data.field.staId;
        staId = parseInt(staId);
        var maimoney = data.field.maimoney;
        maimoney = parseInt(maimoney);
        var maidate = data.field.maidate;
        maidate = new Date(maidate);
        $.ajax({
            url: "/Maintain/update",
            type: "POST",
            dataType:"json",
            async:false,
            data: {
                maiid:maiid,
                mailength:mailength,
                "staff.staid":staId,
                maimoney:maimoney,
                maidate:maidate
            },
            success: function(data){
                if(data.success){
                    window.location.href ="/maintain/maintainList.jsp";
                }else {
                    layer.msg(data.message);
                }
            }
        });
    }
</script>
</html>
